package com.uk.ui.manaxhim;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.uk.data.entities.RoleEnum;
import com.uk.data.entities.User;
import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DateField;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TwinColSelect;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;

public class UserForm extends Form implements ClickListener{

	public static final Object[] COL_ORDER_FOR_FORM = new Object[]{"username","password", "name","surname","startDate","endDate", "locked"};
	
	private ManaxhimPanel manaxhimPanel;
	private Button save = new Button("Ruaj", (ClickListener) this);
	private Button cancel = new Button("Anullo", (ClickListener) this);
	private Button edit = new Button("Ndrysho", (ClickListener) this);
	private Button reset  = new Button("Reset", (ClickListener)this);
	
	private boolean addUserMode = false;
	private User newUser;
	
	public UserForm(ManaxhimPanel manaxhimPanel) {
		super();
		this.manaxhimPanel = manaxhimPanel;
		setWriteThrough(false);
		this.setInvalidCommitted(false);
		HorizontalLayout footer = new HorizontalLayout();
		footer.setSpacing(true);
		footer.addComponent(save);
		footer.addComponent(cancel);
		footer.addComponent(edit);
		footer.addComponent(reset);
		footer.setVisible(false);
		setFooter(footer);
		this.setVisibleItemProperties(COL_ORDER_FOR_FORM);
		
		setFormFieldFactory(new DefaultFieldFactory(){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public Field createField(Item item, Object propertyId,
					Component uiContext) {
				
				Field field = super.createField(item, propertyId, uiContext);
				
				if (propertyId.equals("username")){
					
					TextField tf = (TextField)field;
					tf.setCaption("Perdoruesi");
					tf.setMaxLength(30);
					tf.setRequired(true);
		            tf.setRequiredError("Perdoruesi nuk duhet te jete bosh!");
					tf.setWidth("200px");
					tf.setNullRepresentation("");
					return tf;
				}
				
				if (propertyId.equals("password")){
					PasswordField tf = new PasswordField("Fjalekalimi");
					tf.setMaxLength(20);
					tf.setRequired(true);
		            tf.setRequiredError("Fjalekalimi nuk duhet te jete bosh!");
					tf.setWidth("200px");
					tf.setNullRepresentation("");
					return tf;
				}
				
				if (propertyId.equals("name")){
					TextField tf = (TextField)field;
					tf.setCaption("Emri");
					tf.setMaxLength(30);
					tf.setRequired(true);
		            tf.setRequiredError("Emri nuk duhet te jete bosh!");
					tf.setWidth("200px");
					tf.setNullRepresentation("");
					return tf;
				}
				
				if (propertyId.equals("surname")){
					TextField tf = (TextField)field;
					tf.setCaption("Mbiemri");
					tf.setMaxLength(30);
					tf.setRequired(true);
		            tf.setRequiredError("Mbiemri nuk duhet te jete bosh!");
					tf.setWidth("200px");
					tf.setNullRepresentation("");
					return tf;
				}
				
				if (propertyId.equals("startDate")){
					DateField tf = (DateField)field;
					tf.setCaption("Data e fillimit");
					tf.setDateFormat("dd.MM.yyyy");
					tf.setRequired(true);
		            tf.setRequiredError("Data e fillimit nuk duhet te jete bosh!");
					tf.setWidth("200px");
					tf.setValue(new Date());
					return tf;
				}
				
				if (propertyId.equals("endDate")){
					DateField tf = (DateField)field;
					tf.setCaption("Data e perfundimit");
					tf.setDateFormat("dd.MM.yyyy");
					tf.setRequired(true);
		            tf.setRequiredError("Data e perfundimit nuk duhet te jete bosh!");
					tf.setWidth("200px");
					tf.setValue(new Date());
					return tf;
				}
				
				if (propertyId.equals("locked")){
					CheckBox ch = (CheckBox)field;
					ch.setCaption("E kycur?");
					return ch;
				}

				/*if (propertyId.equals("rolesList")){
					TwinColSelect twinColSelect = new TwinColSelect();
					twinColSelect.addItem(RoleEnum.KONTRATA.name());
					twinColSelect.addItem(RoleEnum.TARIFA.name());
					twinColSelect.addItem(RoleEnum.FATURA.name());
					twinColSelect.addItem(RoleEnum.MANAXHIM.name());
					
					return twinColSelect;
					
				}*/
				
						return field;
				
			}
			
		});
		
	}
	
	
	public void setItemDataSource(Item newDataSource){

		setVisible(true);
		addUserMode = false;
		this.setVisible(true);
		if (newDataSource != null){
			setReadOnly(false);
			List<Object> orderedProperties = Arrays.asList(COL_ORDER_FOR_FORM);
			super.setItemDataSource(newDataSource, orderedProperties);
			
			setReadOnly(true);
			getFooter().setVisible(true);
		}
		else{
			super.setItemDataSource(null);
			this.setVisible(false);
			getFooter().setVisible(false);
		}
		
	}

	
	public void setReadOnly(boolean readOnly){
		super.setReadOnly(readOnly);
		save.setVisible(!readOnly);
		cancel.setVisible(!readOnly);
		edit.setVisible(readOnly);
		reset.setVisible(readOnly);
		
		
	}
	
	public void addUser(){
		
		newUser = new User();
		
		setItemDataSource(new BeanItem<User>(newUser));
		addUserMode = true;
		setReadOnly(false);
		this.getField("startDate").setValue(new Date());
		this.getField("locked").setValue(false);
		
	}
	
	
	@Override
	public void buttonClick(ClickEvent event) {
		Button source = event.getButton();
		this.setComponentError(null);
		try {
			if (source == edit){
				setReadOnly(false);
				this.getField("password").setReadOnly(true);
				
			}
			else if (source == reset){
				try {
					User user = ((BeanItem<User>)this.getItemDataSource()).getBean();
					user.setPassword(user.getUsername());
					user = manaxhimPanel.getFaturaBean().saveUser(user);
					
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			else if (source == cancel) {
				if (addUserMode){
					addUserMode = false;
					setItemDataSource(null);
				}
				else discard();
				setReadOnly(true);
				
			}
			else if (source == save){
				try {
					setReadOnly(false);
					commit();
					
					User user = null;
					if (addUserMode){
						user = manaxhimPanel.getFaturaBean().saveUser(((BeanItem<User>)this.getItemDataSource()).getBean());
						Item  addedItem = manaxhimPanel.getUserContainer().addItem(user);
						setItemDataSource(addedItem);
						addUserMode = false;
					}
					else {
						user = manaxhimPanel.getFaturaBean().updateUser(((BeanItem<User>)this.getItemDataSource()).getBean());
					}
					setReadOnly(true);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

}
